package com.yunhai.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunhai.common.dto.GlobalException;
import com.yunhai.common.dto.PaginationVo;
import com.yunhai.common.utils.ObjectUtil;
import com.yunhai.common.utils.ReflectionCopy;
import com.yunhai.model.entity.fms.FmsMaterialConfigEntity;
import com.yunhai.model.entity.tms.TmsStoreCategoryEntity;
import com.yunhai.model.vo.tms.TmsStoreCategoryQuery;
import com.yunhai.model.vo.tms.TmsStoreCategoryVo;
import com.yunhai.store.mapper.FmsCodeDictMapper;
import com.yunhai.store.mapper.TmsStoreCategoryMapper;
import com.yunhai.store.service.TmsStoreCategoryService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class TmsStoreCategoryServiceImpl implements TmsStoreCategoryService {

    @Resource
    private TmsStoreCategoryMapper tmsStoreCategoryMapper;
    @Resource
    private FmsCodeDictServiceImpl fmsCodeDictService;


    /**
     * 获取 TmsStoreCategory 分页列表
     */
    @Transactional
    public IPage getTmsStoreCategoryPagination(PaginationVo paginationVo, TmsStoreCategoryQuery TmsStoreCategoryQuery ) throws Exception {

        IPage page = new Page( paginationVo.getPageIndex(), paginationVo.getPageSize() );
        LambdaQueryWrapper<TmsStoreCategoryEntity> lqs = new LambdaQueryWrapper();
        lqs.likeRight(TmsStoreCategoryQuery.getName() !=null, TmsStoreCategoryEntity::getName, TmsStoreCategoryQuery.getName());
        lqs.orderByDesc( TmsStoreCategoryEntity::getCreateTime );
        tmsStoreCategoryMapper.selectPage(page, lqs);

        List<TmsStoreCategoryEntity> record = page.getRecords();
        if( !ObjectUtil.isNotEmptyList(record)){
            return page;
        }
        return page;
    }


    /**
     * 获取 TmsStoreCategory 所有数据
     *
     */
    @Transactional
    public List<TmsStoreCategoryEntity> getTmsStoreCategoryAllList( TmsStoreCategoryQuery TmsStoreCategoryQuery ) throws Exception {

        LambdaQueryWrapper<TmsStoreCategoryEntity> lqs = new LambdaQueryWrapper();
        List<TmsStoreCategoryEntity> list = tmsStoreCategoryMapper.selectList(lqs);


        return list;
    }


    /**
     * 获取 TmsStoreCategory 详情
     */
    @Transactional
    public TmsStoreCategoryEntity getTmsStoreCategoryDetail( Long id) throws Exception {

        TmsStoreCategoryEntity storeCategory = tmsStoreCategoryMapper.selectById(id);
        return storeCategory;
    }


    /**
     * 新增 TmsStoreCategory
     * */
    @Transactional
    public void addTmsStoreCategory(TmsStoreCategoryVo tmsStoreCategoryVo) throws Exception {

        TmsStoreCategoryEntity tmsStoreCategory = new TmsStoreCategoryEntity();
        ReflectionCopy.copyProperties( tmsStoreCategory, tmsStoreCategoryVo );
        tmsStoreCategory.setStatus(1);
        tmsStoreCategoryMapper.insert(tmsStoreCategory);

        fmsCodeDictService.updateById(7L, tmsStoreCategoryVo.getNumber());

        return;
    }


    /**
     * 更新 TmsStoreCategory
     * */
    @Transactional
    public void updateTmsStoreCategory( TmsStoreCategoryVo TmsStoreCategoryVo) throws Exception {

        if( TmsStoreCategoryVo.getId() == null ){
            throw GlobalException.gen(10021, "id不存在");
        }
        TmsStoreCategoryEntity TmsStoreCategory = new TmsStoreCategoryEntity();
        ReflectionCopy.copyProperties( TmsStoreCategory, TmsStoreCategoryVo );
        tmsStoreCategoryMapper.updateById(TmsStoreCategory);
        return;
    }

    /**
     * 删除 TmsStoreCategory
     * */
    @Transactional
    public void batchRemoveTmsStoreCategory( List<Long> ids ) throws Exception {

        if( !ObjectUtil.isNotEmptyList(ids) ){
            throw GlobalException.gen(10011, "要删除的id不存在");
        }
        tmsStoreCategoryMapper.deleteBatchIds(ids);
    }
}
